<?php
/*
 * kernel/object.file.php 
 * 
 * Web application interface
 * 
 * Copyright (C) 2010, Taras Kudla
 * 
 */
class api extends kernel{
	
	public  $A_LISTENER=false;
	private $ResponseType="xml";
	
	public function init(){
		
	}
		
public function listen(){
	# language detection
	//if(isset($_COOKIE["ln"]) && in_array($_COOKIE["ln"],$this->core->SysLanguages) ){
		//$this->core->Language=$_COOKIE["ln"];
	//}
	$this->core->session;
	$this->core->user;
	# response type detection
	if( isset($_GET["ResponseType"])){
		switch($_GET["ResponseType"]){
			case "xml":$response="xml";break;
			case "html":$response="html";break;
			case "json":$response="json";break;
			default: $response = "xml";break;
		}
	} else $response = "xml";
	$this->ResponseType = $response;
	if( isset($_GET["GetAction"]) && !empty($_GET["GetAction"])){
		if( preg_match("/^([A-z0-9_]{1,30})-([A-z0-9_]{1,30})$/i",$_GET["GetAction"],$tmp)){
			$mod = $tmp[1];
			$action = $tmp[2];
			$data = $this->core->base->callaction($mod,$action);
			$this->GetResponse($data);
		}
	}	
	if(isset($_GET["Action"]) && !empty($_GET["Action"])){
		if( preg_match("/^([A-z0-9_]{1,30})-([A-z0-9_]{1,30})$/i",$_GET["Action"],$tmp)){
			$hash=$this->core->base->GetHash($tmp[1],$tmp[2]);
		}else if( preg_match("/^([A-z0-9_]{"._HASH_LENGTH."})$/i",$_GET["Action"],$tmp) ){
			$hash=$tmp[0];
		} else {
			print "Error in query.";
			return null;
		}
		$this->core->base->OutputKeys();
		$this->core->base->LoadFunction($hash);		
		
		return $this->ProcessResponse();
	}
	//$hash=$this->core->base->GetHash($tmp[1],$tmp[2]);
	//$result=$this->core->base->LoadFunction($hash,YES);
	//$this->GetResponse($result);
	return true;
}
	
	public function CallRedirect($link){
		
		header("Content-type:text/xml;\n");
		$x="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
		$x.="<root>\n\n";
		$x.="<redirect>".$link."</redirect>";
		$x.="</root>";
		print $x;
	}
		
public function ProcessResponse(){
	# XML Response Type
	if(strtolower($this->ResponseType)=="xml"){
		header("Content-type:text/xml;\n");
	$x="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<root>\n\n";
	$outvalues = $this->core->base->getoutvalues();
	$outdata  =$this->core->base->getoutdata();
	foreach($outvalues as $k=>&$v){
		if(empty($v))continue;
		$x.="\n<area name=\"".strtolower($k)."\">";
		asort($v,SORT_NUMERIC);
		foreach ($v as $a=>$b){
			$x.="<data action=\"".$a."\" weight=\"".$b."\"><![CDATA[".$outdata[$a]["data"]."]]></data>";
		}
		$x.="</area>\n";
	}
	$time=$this->core->runtimepoint();
	$x.="<loaded>".$time."</loaded></root>";
	print $x;
	}
}
	
	public function GetResponse(&$data){
		
		if(strtolower($this->ResponseType)=="xml"){
			header("Content-type:text/xml;\n");
			print "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<root><data><![CDATA[".$data."]]></data></root>\n\n";
		}
		
		if(strtolower($this->ResponseType)=="html"){
			print $data;
		}

		}
	
	}
?>